package infoexract.parseall;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;

import datastruct.document.BiomedDocument;

public class AllDirFilesParsers {
	public void ParseDir(String dir, String outputdir)
	{
		File root = new File(dir);
		File outputRoot = new File(outputdir);
		Vector<String> finishSet = new Vector<String>();
		try {
			BufferedReader freader = new BufferedReader(new FileReader("data/finishlog/allparsefinish.log"));
			String line;
			while((line=freader.readLine())!=null)
			{
				if(!finishSet.contains(line)) finishSet.add(line);
			}
			freader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(!root.exists()||!root.isDirectory()) 
		{
			System.out.println("Wrong dir to parse!");
			return;
		}
		if(!outputRoot.exists()||!outputRoot.isDirectory()) 
		{
			System.out.println("Wrong output dir!");
			return;
		}
		File[] files = root.listFiles();
		int counter = 0;
		for(File file : files)
		{
			if(file.getName().endsWith("~")) continue;
			counter++;
			System.out.print("#"+counter+" Processing file "+file.getAbsolutePath()+" ....");
			if(!finishSet.contains(file.getName()))
			{
				BiomedDocument doc = new BiomedDocument();
				doc.ReadFromRawTextFile(file.getAbsolutePath());
				doc.deepParseAllSentence();
				doc.WriterToParsedTextFile(outputdir+file.getName()+".parsed");
				finishSet.add(file.getName());
				try {
					BufferedWriter fwriter = new BufferedWriter(new FileWriter("data/finishlog/allparsefinish.log",true));
					fwriter.write(file.getName());
					fwriter.newLine();
					fwriter.close();
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			System.out.println("Finished!");
		}
		
	}
}
